import 'dart:async';

import 'package:flutter/material.dart';

// ignore: must_be_immutable
class MySwiper extends StatefulWidget {
  MySwiper({
    Key? key,
    this.width = double.infinity,
    this.height = 200,
    this.scrollDirection = Axis.horizontal,
    required this.swipers,
  }) : super(key: key);

  double width;

  double height;

  Axis scrollDirection;

  List<Widget> swipers;

  @override
  _MySwiperState createState() => _MySwiperState();
}

class _MySwiperState extends State<MySwiper> {
  late PageController _pageController;

  late Timer _timer;

  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();

    _pageController = PageController(initialPage: 0);
    _initTimer();
  }

  @override
  void dispose() {
    super.dispose();

    _pageController.dispose();
    _timer.cancel();
  }

  void _initTimer() {
    _timer = Timer.periodic(const Duration(seconds: 5), (timer) {
      _pageController.animateToPage(
        (_currentIndex + 1) % widget.swipers.length,
        duration: const Duration(milliseconds: 500),
        curve: Curves.linear,
      );
    });
  }

  Widget _getPageControl() {
    // return Positioned(
    //   left: 0,
    //   right: 0,
    //   bottom: 4,
    //   child: Row(
    //     mainAxisAlignment: MainAxisAlignment.center,
    //     children: List.generate(
    //         widget.swipers.length,
    //         (index) => Container(
    //               width: 12,
    //               height: 12,
    //               margin: const EdgeInsets.all(5),
    //               decoration: BoxDecoration(
    //                 shape: BoxShape.circle,
    //                 color: _currentIndex == index ? Colors.green : Colors.grey,
    //               ),
    //             )).toList(),
    //   ),
    // );
    return Align(
      widthFactor: widget.width,
      alignment: Alignment.bottomCenter,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: List.generate(
            widget.swipers.length,
            (index) => GestureDetector(
                  onTapUp: (details) {
                    print(details);
                    _timer.cancel();
                    _initTimer();
                    _pageController.animateToPage(
                      index,
                      duration: const Duration(milliseconds: 500),
                      curve: Curves.linear,
                    );
                  },
                  child: Container(
                    width: 12,
                    height: 12,
                    margin: const EdgeInsets.all(5),
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color:
                          _currentIndex == index ? Colors.green : Colors.grey,
                    ),
                  ),
                )).toList(),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    print(widget.swipers);
    return SizedBox(
      height: widget.height,
      child: Stack(
        children: [
          SizedBox(
            width: widget.width,
            height: widget.height,
            child: PageView(
              scrollDirection: widget.scrollDirection,
              controller: _pageController,
              onPageChanged: (index) {
                setState(() {
                  _currentIndex = index;
                });
              },
              children: widget.swipers,
            ),
          ),
          _getPageControl(),
        ],
      ),
    );
  }
}
